<?php

namespace Admin\Controller;

class QuickOrderController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("QuickOrder");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        $this->assign("begin_time", $_REQUEST["qo#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["qo#create_time-elt"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $orderList = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("defaultEnum", C("INVOICE_TYPE"));
        $this->assign("statusEnum", C("FINISHED_ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }


    public function endDelivery() {

        $id = $_REQUEST["id"];
        $quickOrder = $this->currentModel->queryById($id);

        $this->assign("quickOrder", $quickOrder);
        $this->display();

    }

    public function end() {


        $id = $_REQUEST["id"];

        $quickOrder = $this->currentModel->queryById($id);

        $quickOrder["status"] = C("FINISHED_ORDER_STATUS.END_DELIVERY");
        $quickOrder["delivery_number"] = $_REQUEST["deliveryNumber"];
        $quickOrder["express_company"] = $_REQUEST["expressCompany"];
        $quickOrder["outstock_time"] = getCurrentDate();
        $quickOrder["delivery_id"] = $_SESSION['ADMIN_ID'];

        $this->currentModel->updateInfo($quickOrder);

        //推送留言信息到用户和管理员
        $finishedProduct = D("FinishedProduct")->queryById($quickOrder["finished_product_id"]);
        $customer = D("Customer")->queryById($quickOrder["customer_id"]);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/finishedProduct/quickOrderList/?id=" . $quickOrder["quick_order_id"]."&status=1";
        $notifyData["first"] = "快速成品订单已发货。发货人：" . $user["admin_name"];
        $notifyData["keyword1"] = "[" . $finishedProduct["name"] . "], 共 " . $quickOrder["number"] . " 件";
        $notifyData["keyword2"] = "[" . $quickOrder["delivery_type"] . "]，订单人：" . $customer["nickname"];
        $notifyData["keyword3"] = $_REQUEST["deliveryNumber"];
        if(!isNullOrEmpty($_REQUEST["expressCompany"])){
            $notifyData["keyword3"] .= " [" . $_REQUEST["expressCompany"] . "]";
        }
        $notifyData["remark"] = "订单总额：" . $quickOrder["total_price"] . " 元";

//        $adminRoleArr = array("1","8","10");
//        $openidArray = getNotifyArr($adminRoleArr, $user["enterprise_code"]);
        $openidArray = getNotifyArrByName("endQuickOrder", $user["enterprise_code"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            finishedOrderEndNotify($notifyData);
        }

        $notifyData["openid"] = $customer["openid"];
        $notifyData["first"] = "您的成品订单已发货。" ;
        finishedOrderEndNotify($notifyData);

        $this->success("已标记成品订单为已出库状态", U("quickOrder/index"));
    }

}